package cn.xyz.mianshi.service.impl;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;

import cn.xyz.mianshi.model.PageResult;
import cn.xyz.mianshi.utils.SKBeanUtils;
import cn.xyz.mianshi.vo.Bank;
import cn.xyz.repository.mongo.MongoRepository;
@Service
public class BankManagerIml extends MongoRepository<Bank,ObjectId>{

	@Override
	public Datastore getDatastore() {
		// TODO Auto-generated method stub
		 return SKBeanUtils.getLocalSpringBeanManager().getDatastore();
	}

	@Override
	public Class<Bank> getEntityClass() {
		return Bank.class;
	}
	public  PageResult<Bank> queryUserBankList(Integer userId,int page,int limit)
	{
		PageResult<Bank> result = new PageResult<Bank>();
		Query<Bank> query = getDatastore().createQuery(getEntityClass()).filter("userID",userId);
		List<Bank> recordList = query.asList(pageFindOption(page,limit,1));
		result.setCount(query.count());
		result.setData(recordList);
		return result;
	}

	/**
	 * 该银行卡是否已经被绑定
	 * @param BankCode
	 * @return
	 */
	public boolean isBind(String BankCode)
	{
		Query<Bank> query = getDatastore().createQuery(getEntityClass()).filter("bankCode",BankCode);
		if(query.count()>0)
		{
			return true;
		}
		return false;
	}
	/**
	 * 银行卡是否为该用户所有
	 * @param userId
	 * @param BankId
	 * @return
	 */
	public boolean isOwner(Integer userId,String BankId)
	{
		Query<Bank> query = getDatastore().createQuery(getEntityClass()).filter("userID",userId).filter("id", new ObjectId(BankId));
		if(query.count()>0)
		{
			return true;
		}
		return false;
	}
}
